﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ProjectSem3
{
    public partial class WebForm6 : System.Web.UI.Page
    {
        private dataDataContext dc = new dataDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
            {
                this.Repeater_Product.DataSource = this.dc.Products.ToList();
                this.Repeater_Product.DataBind();
                this.hienthidulieu();
            }
        }

        protected void Repeater_Product_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            HiddenField productID = e.Item.FindControl("productID") as HiddenField;
            var img = this.dc.ImagesPros.Where(i => i.ProductID == Int32.Parse(productID.Value)).Skip(0).Take(1);
            Image image = e.Item.FindControl("image") as Image;
            foreach (var a in img)
            {
                image.ImageUrl = "~/images/Product/" + a.NameImg;
            }
        }
        private void hienthidulieu()
        {
            List<Item> cart = (List<Item>)Session["Cart"];
            this.RepeaterCart.DataSource = cart;
            this.RepeaterCart.DataBind();
            this.LabelTotal.Text = total().ToString();
        }
        private double total()
        {
            List<Item> cart = (List<Item>)Session["Cart"];
            double t = 0;
            for (int i = 0; i < cart.Count; i++)
                t += (double)cart[i].Pro.Price.Value * cart[i].Quantity;
            return t;
        }
        protected void LinkButtonDeleteItem_Click(object sender, EventArgs e)
        {
            LinkButton LinkButtonDeleteItem = (LinkButton)sender;
            HiddenField HiddenFieldProductID = (HiddenField)LinkButtonDeleteItem.FindControl("HiddenFieldProductID");
            int productID = Convert.ToInt32(HiddenFieldProductID.Value);

            int index = this.IndexOfItem(productID);
            List<Item> cart = (List<Item>)Session["Cart"];
            cart.RemoveAt(index);
            Session["Cart"] = cart;
            Response.Redirect("cart.aspx");

        }
        private int IndexOfItem(int productID)
        {
            List<Item> cart = (List<Item>)Session["Cart"];
            for (int i = 0; i < cart.Count; i++)
            {
                if (cart[i].Pro.ProductID == productID)
                    return i;
            }
            return -1;
        }
        protected void ButtonUpdateSoluong_Click(object sender, EventArgs e)
        {
            Button ButtonUpdateSoluong = (Button)sender;
            HiddenField HiddenFieldProductID = (HiddenField)ButtonUpdateSoluong.FindControl("HiddenFieldProductID");
            int productID = Convert.ToInt32(HiddenFieldProductID.Value);
            int index = this.IndexOfItem(productID);
            TextBox texboxSoluong = (TextBox)ButtonUpdateSoluong.FindControl("TextBoxSoluong");

            List<Item> cart = (List<Item>)Session["Cart"];
            cart[index].Quantity = Convert.ToInt32(texboxSoluong.Text);
            Session["Cart"] = cart;
            Response.Redirect("cart.aspx");

        }
        protected void AcheckOut_Click(object sender, EventArgs e)
        {
            if (Session["username"] == null)
                Response.Redirect("myaccount.aspx");
            else
            {

            }
        }

        protected void RepeaterCart_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {

            HiddenField productID = e.Item.FindControl("HiddenFieldProductID") as HiddenField;
            var img = this.dc.ImagesPros.Where(i => i.ProductID == Int32.Parse(productID.Value)).Skip(0).Take(1);
            Image image = e.Item.FindControl("imgcart") as Image;
            foreach (var a in img)
            {
                image.ImageUrl = "~/images/Product/" + a.NameImg;
            }
        }
    }
}